#include "PlayerStateAirDash.h"
#include "Player.h"

PlayerStateAirDash::PlayerStateAirDash(Player* player, sf::Sound* dashSFX) : 
PlayerState(player),
_dashSFX(dashSFX)
{}

PlayerStateAirDash::~PlayerStateAirDash(void){}

void PlayerStateAirDash::activate()
{
	_player->dash();
	_player->removeFlag(GRAVITY_AFFECTED);
	_player->getAnimationMgr().setAnimation("dash");
	_dashDirection = _player->getFlip();
	_dashTimer.restart();
	_dashSFX->play();
	_player->_velocity.y = 0.0f;
}

void PlayerStateAirDash::deactivate()
{
	_player->setFlag(GRAVITY_AFFECTED);
	_player->resetDash(false);
	if (_dashSFX->Playing)
		_dashSFX->stop();
}

int PlayerStateAirDash::update()
{
	const sf::Int32 maxDashLength = 500;

	if (_player->damageReceived())
		return PSRV_TAKE_DAMAGE;

	if (_dashDirection == 1.0f && _player->getFlip() == -1.0f)
	{
		return PSRV_FALLING;
	}
	else if (_dashDirection == -1.0f && _player->getFlip() == 1.0f)
	{
		return PSRV_FALLING;
	}

	if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_SHOOT, 0), 0))
	{
		return PSRV_ATTACK_AIR;
	}

	if (_player->getAnimationMgr().getCurrentID() == "dash")
	{
		_player->applyVelocity(VA_FLIP_BASED);
	}

	if (_player->getControlState()->isKeyReleased(CControls::Key(CTRL_DASH, 0), 0))
	{
		return PSRV_FALLING;
	}

	if (_dashTimer.getElapsedTime().asMilliseconds() >= _player->getAirDashLength())
	{
		return PSRV_FALLING;
	}

	if (((_player->flags() & COLLISION_HIT_LEFT) == COLLISION_HIT_LEFT) || ((_player->flags() & COLLISION_HIT_RIGHT) == COLLISION_HIT_RIGHT))
	{
		return PSRV_FALLING;
	}

	return PSRV_NOTHING;
}